package com.bootdo.wx.controller;

import com.bootdo.common.service.JPushService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.domain.CmsNoticeSysDO;
import com.bootdo.wx.service.CmsNoticeSysService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统公告
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-07-04 17:10:56
 */

@Controller
@RequestMapping("/wx/cmsNoticeSys")
public class CmsNoticeSysController {
    @Autowired
    private CmsNoticeSysService cmsNoticeSysService;
    @Autowired
    private JPushService jPushService;

    @GetMapping()
    @RequiresPermissions("wx:cmsNoticeSys:cmsNoticeSys")
    String CmsNoticeSys() {
        return "wx/cmsNoticeSys/cmsNoticeSys";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("wx:cmsNoticeSys:cmsNoticeSys")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<CmsNoticeSysDO> cmsNoticeSysList = cmsNoticeSysService.list(query);
        int total = cmsNoticeSysService.count(query);
        PageUtils pageUtils = new PageUtils(cmsNoticeSysList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("wx:cmsNoticeSys:add")
    String add() {
        return "wx/cmsNoticeSys/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("wx:cmsNoticeSys:edit")
    String edit(@PathVariable("id") String id, Model model) {
        CmsNoticeSysDO cmsNoticeSys = cmsNoticeSysService.get(id);
        model.addAttribute("cmsNoticeSys", cmsNoticeSys);
        return "wx/cmsNoticeSys/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("wx:cmsNoticeSys:add")
    public R save(CmsNoticeSysDO cmsNoticeSys) {
        String id = UuidUtil.get32UUID();
        cmsNoticeSys.setId(id);
        cmsNoticeSys.setCreateAt(new Date());
        cmsNoticeSys.setDelFlag(0);
        if (cmsNoticeSysService.save(cmsNoticeSys) > 0) {
            //jPushService.allPush(cmsNoticeSys.getTitle(),1,id);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("wx:cmsNoticeSys:edit")
    public R update(CmsNoticeSysDO cmsNoticeSys) {
        cmsNoticeSysService.update(cmsNoticeSys);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("wx:cmsNoticeSys:remove")
    public R remove(String id) {
        if (cmsNoticeSysService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("wx:cmsNoticeSys:batchRemove")
    public R remove(@RequestParam("ids[]") String[] ids) {
        cmsNoticeSysService.batchRemove(ids);
        return R.ok();
    }

}